export const mixin = {
  props: {
    label: String,
    prop: String,
    required: Boolean,
    placeholder: String,
    rules: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    required() {
      this.setRules()
    }
  },
  data() {
    return {
      rulesList: [],
      placeholderText: '请选择'
    }
  },
  created() {
    this.setRules()
  },
  mounted() {
    if (!this.placeholder) {
      if (document.getElementsByClassName('el-form-item__label').length) {
        if (window.getComputedStyle(document.getElementsByClassName('el-form-item__label')[0], null).display !== 'none') {
          return
        }
      }
      if (this.label) {
        if (this.label.indexOf(':') !== -1) {
          this.placeholderText = this.label.split(':')[0]
        } else if (this.label.indexOf('：') !== -1) {
          this.placeholderText = this.label.split('：')[0]
        }
      }
    }
  },
  methods: {
    setRules() {
      this.rulesList = [].concat(this.rules)
      if (this.required) {
        this.rulesList.push({ required: true, message: ' ', trigger: 'change' })
      }
    }
  }
}
